/*
 * Copyright (c) United Binary LLC.  All rights reserved.
 * 
 * This code is licensed under the MIT License
 * 
 * SEE: http://harnessit.codeplex.com/license
 * 
 */
using System;
using System.Windows.Forms;
using System.Diagnostics;

namespace UnitedBinary.Core.Windows.Forms.Persistence
{
	/// <include file='Persistence.xml' path='/Docs/PersistentSplitterState/Class/*'/>
	public class PersistentSplitterState
	{
		private string splitPosValue = "Splitter Position";
		private Splitter s = null;
		private string keyPath = string.Empty;

		/// <include file='Persistence.xml' path='/Docs/PersistentSplitterState/splitPos/*'/>
		protected int splitPos = 200;

		/// <include file='Persistence.xml' path='/Docs/PersistentSplitterState/ctor/*'/>
		public PersistentSplitterState(Splitter splitter, string subKeyBaseName)
		{
			if (subKeyBaseName == null || subKeyBaseName.Length == 0)
			{
				System.ArgumentException e = new
					System.ArgumentException("Invalid SubKeyBaseName", "SubKeyBaseName");
				e.Source = "PersistentSplitterState class";
				throw e;
			}

			if (splitter == null)
			{
				System.ArgumentException e = new
					System.ArgumentException("Invalid Splitter splitter", "splitter");
				e.Source = "PersistentSplitterState class";
				throw e;
			}
			s = splitter;
			splitPos = s.SplitPosition;

			char[] chars = {'\\'};
			subKeyBaseName = subKeyBaseName.TrimStart(chars);
			subKeyBaseName = subKeyBaseName.TrimEnd(chars);
			keyPath = "Software\\" + subKeyBaseName + "\\" + "PersistentSplitterState";

			Debug.WriteLine("PersistentSplitterState: Using registry key: HKEY_CURRENT_USER\\" + keyPath);
		}

		/// <include file='Persistence.xml' path='/Docs/PersistentSplitterState/SplitPosition/*'/>
		public int SplitPosition
		{
			get
			{
				try
				{
					Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(keyPath);
					splitPos = (int)key.GetValue(splitPosValue);

					Debug.WriteLine("PersistentSplitterState: Read SplitPosition from registry: " + splitPos);
				}
				catch
				{
					Debug.WriteLine("PersistentSplitterState Warning: Could not read registry key, SplitPosition has never been set?");
					Debug.WriteLine("PersistentSplitterState Warning: Using default value " + splitPos);
				}

				return splitPos;
			}
			set
			{
				Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(keyPath);
				key.SetValue(splitPosValue,value);
				Debug.WriteLine("PersistentSplitterState: Wrote SplitPosition to registry: " + value);
			}
		}

		/// <include file='Persistence.xml' path='/Docs/PersistentSplitterState/Initialize/*'/>
		public void Initialize()
		{
			//
			// The splitter is loading.  Read the dimensions from the (persistent)
			// properties of our window persisting class.
			//
			s.SplitPosition = SplitPosition;
		}

		/// <include file='Persistence.xml' path='/Docs/PersistentSplitterState/Save/*'/>
		public void Save()
		{
			SplitPosition = s.SplitPosition;
		}
	}
}
